package leetcode.programmer;

public class ReplaceSpaces {

    public static void main(String[] args) {
        System.out.println(replaceSpaces("ds sdfs afs sdfa dfssf asdf             ", 27));
    }
    public static String replaceSpaces(String s, int length) {
        int i = length - 1;
        int j = s.length() - 1;
        char[] chars = s.toCharArray();
        while (i>=0) {
            if (chars[i]==' '){
                chars[j--]='0';
                chars[j--]='2';
                chars[j--]='%';
            }else {
                chars[j--]=chars[i];
            }
            i--;
        }
        return String.valueOf(chars,j+1, s.length()-j-1);
    }
}
